import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import set_node_cls from '@dfeidao/atom-web/ui/set-node-cls';

import format from '@dfeidao/atom-web/dt/format';
import get from '@dfeidao/atom-web/global/get';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
import p004 from './p004';
import p005 from './p005';
import p007 from './p007';
import p008 from './p008';
import p010 from './p010';
import p011 from './p011';

export default async function a003(fd: IFeidaoAiBrowserComponent, e: Event) {
	// todo

	const word_knowledge = fd.data.node.querySelector<HTMLDivElement>('[data-id="word_knowledge"]');
	const knowledge = fd.data.node.querySelector<HTMLDivElement>('[data-id="knowledge"]');

	const technology = fd.data.node.querySelector<HTMLDivElement>('[data-id="technology"]');
	const tyl = fd.data.node.querySelector<HTMLDivElement>('[data-id="tly"]');

	const business = fd.data.node.querySelector<HTMLDivElement>('[data-id="business"]');
	const bus = fd.data.node.querySelector<HTMLDivElement>('[data-id="bus"]');

	const defined = fd.data.node.querySelector<HTMLDivElement>('[ data-id="user-defined"]');
	const ded = fd.data.node.querySelector<HTMLDivElement>('[data-id="u_defined"]');


	const action = e.currentTarget as HTMLElement;
	const actions = action.innerText;

	const dta = get('dta');
	const staff_n = dta.staff_no;

	if (action) {
		if (actions === '工单知识库') {
			// 工单知识库显示
			word_knowledge.setAttribute('class', 'display-');
			set_node_cls(knowledge, 'bg-themef', true);

			// 技术类隐藏
			set_node_cls(technology, 'display-n', true);
			set_node_cls(tyl, 'bg-themef', false);

			// 商务类隐藏
			set_node_cls(business, 'display-n', true);
			set_node_cls(bus, 'bg-themef', false);
			// 自定义隐藏
			set_node_cls(defined, 'display-n', true);
			set_node_cls(ded, 'bg-themef', false);

		}
		if (actions === '技术类文件') {
			// 工单知识库隐藏
			word_knowledge.setAttribute('class', 'display-n');
			set_node_cls(knowledge, 'bg-themef', false);
			// 技术类显示
			set_node_cls(technology, 'display-n', false);
			set_node_cls(tyl, 'bg-themef', true);

			// 商务类隐藏
			set_node_cls(business, 'display-n', true);
			set_node_cls(bus, 'bg-themef', false);
			// 自定义隐藏
			set_node_cls(defined, 'display-n', true);
			set_node_cls(ded, 'bg-themef', false);
			const re = await nodejs<{
				res: Array<{
					knowledge_name: string, knowledge_explain: string,
					create_time: number, knowledge_class: string
				}>
			}>('knowledge-base/zj-000001/s003', {
				filter: {
					staff_no: staff_n
				}
			});
			// 日期转格式
			const ral = re.res.map((n) => {
				return { ...n, create_time: format(n.create_time, 2) };
			});


			if (re.res.length > 0) {
				render(fd, ral, p004, 'p004', 'inner');

			} else {
				render(fd, '', p005, 'p005', 'inner');
			}
		}
		if (actions === '商务类文件') {
			// 工单知识库隐藏
			word_knowledge.setAttribute('class', 'display-n');
			set_node_cls(knowledge, 'bg-themef', false);
			// 商务类显示
			set_node_cls(business, 'display-n', false);
			set_node_cls(bus, 'bg-themef', true);
			// 技术类隐藏
			set_node_cls(technology, 'display-n', true);
			set_node_cls(tyl, 'bg-themef', false);
			// 自定义隐藏
			set_node_cls(defined, 'display-n', true);
			set_node_cls(ded, 'bg-themef', false);
			const res = await nodejs<{
				res: Array<{
					knowledge_no: string, knowledge_name: string, knowledge_explain: string,
					create_time: number, knowledge_class: string
				}>
			}>('knowledge-base/zj-000001/s006', {
				filter: {
					staff_no: staff_n
				}
			});


			const ral = res.res.map((n) => {
				return { ...n, create_time: format(n.create_time, 2) };
			});

			if (res.res.length > 0) {
				render(fd, ral, p007, 'p007', 'inner');
				// render(fd,,p009,'p009','inner');
			} else {
				render(fd, ' ', p008, 'p008', 'inner');
			}


		}
		if (actions === '自定义知识库') {
			// 工单知识库隐藏
			word_knowledge.setAttribute('class', 'display-n');
			set_node_cls(knowledge, 'bg-themef', false);
			// 自定义显示
			set_node_cls(defined, 'display-n', false);
			set_node_cls(ded, 'bg-themef', true);

			// 技术类隐藏
			set_node_cls(technology, 'display-n', true);
			set_node_cls(tyl, 'bg-themef', false);
			// 商务类隐藏
			set_node_cls(business, 'display-n', true);
			set_node_cls(bus, 'bg-themef', false);
			const res = await nodejs<{ res: Array<{ create_time: number }> }>('knowledge-base/zj-000001/s008', {
				filter: {
					staff_no: staff_n
				}
			});
			const ral = res.res.map((n) => {
				return { ...n, create_time: format(n.create_time, 2) };
			});

			if (res.res.length > 0) {
				render(fd, ral, p010, 'p010', 'inner');
			} else {
				render(fd, res, p011, 'p011', 'inner');
			}

		}
	}
}
